/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-08
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.CompanyCategory;
import com.rzico.basics.entity.ProductCategory;
import com.rzico.basics.model.CategoryCompanyVo;
import com.rzico.exception.CustomException;
import com.rzico.util.StringUtils;
import com.rzico.util.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.CompanyCategoryMapper;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * <pre>
 * 企业分类业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class CompanyCategoryService extends BaseServiceImpl<CompanyCategory, String> {

    @Autowired
    private CompanyCategoryMapper companyCategoryMapper;

    @Autowired
    TreeUtils<CompanyCategory> treeUtils;

    @Override
    public BaseMapper<CompanyCategory, String> getMapper() {
        return companyCategoryMapper;
    }



    /**
     * 排序
     *
     * @param items
     *            排序
     * @return 排序
     */
    private List<CompanyCategory> sort(List<CompanyCategory> items) {
        List<CompanyCategory> result = new ArrayList<CompanyCategory>();
        if (items != null) {
            Collections.sort(items, new Comparator<CompanyCategory>() {
                public int compare(CompanyCategory n1, CompanyCategory n2) {
                    if (n1.getOrders()==null) {
                        return -1;
                    }
                    if (n2.getOrders()==null) {
                        return 1;
                    }
                    return n1.getOrders().compareTo(n2.getOrders());
                }
            });
            for (CompanyCategory item : items) {
                result.add(item);
                item.setChildrens(sort(item.getChildrens()));

            }
        }
        return result;
    }



    /**
     *
     * @param params
     * @return
     */
    public List<CompanyCategory> selectTree(Map<String, Object> params) {
        Example example = new Example(CompanyCategory.class);
        criteriaBuilderMap(example, params);
        List<CompanyCategory> list = companyCategoryMapper.selectByExample(example);
        try {
            return sort(treeUtils.buildTree(list));
        } catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertUseGeneratedKeys(CompanyCategory companyCategory) {
        //数据库字段存储不能为空，先存个逗号
        companyCategory.setTreePath(",");
        int num = super.insertUseGeneratedKeys(companyCategory);
        if (num > 0){
            if (null != companyCategory.getParentId() && 0 != companyCategory.getParentId()){
                setTreePath(companyCategory);
            }else{
                companyCategory.setTreePath(StringUtils.TREE_PATH_SEPARATOR + companyCategory.getId() + StringUtils.TREE_PATH_SEPARATOR);
            }
            updateByPrimaryKeySelective(companyCategory);
        }
        return num;
    }

    /**
     * 生成treePath
     * @param companyCategory
     */
    public void setTreePath(CompanyCategory companyCategory){
        CompanyCategory parent = this.selectByPrimaryKey(companyCategory.getParentId());
        if (null != parent){
            companyCategory.setTreePath(parent.getTreePath() + companyCategory.getId() + StringUtils.TREE_PATH_SEPARATOR);
        }else{
            companyCategory.setTreePath(StringUtils.TREE_PATH_SEPARATOR + companyCategory.getId() + StringUtils.TREE_PATH_SEPARATOR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CompanyCategory companyCategory) {
        //todo 查找当前节点的treepath
        if (null != companyCategory.getParentId()){
            setTreePath(companyCategory);
        }
        int num = super.updateByPrimaryKeySelective(companyCategory);
        if (num > 0){
            //更新子节点的treePath
            List<CompanyCategory> list = companyCategoryMapper.selectAllChildren(companyCategory);
            if (null != list && list.size() > 0){
                for (CompanyCategory companyCategory1 : list){
                    setTreePath(companyCategory1);
                    super.updateByPrimaryKeySelective(companyCategory1);
                }
            }
        }
        return num;
    }

}
